<?php

namespace App\Winged\Patients\Controllers;

use App\Winged\Patients\Api\Kase;
use App\Winged\Components\Content;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class CaseController extends BaseController
{


    /**
     * @param null $id 如果id为null ,则获取所有的病例列表数据，否则获取该患者的列表
     */
    public function index(Request $request)
    {
        $currentPage = $request->input('page', $this->defaultPage);
        $case_name   = $request->input('case_name', null);
        $mine        = $request->input('mine', 0);
        if (is_null($case_name)) {
            $data = Kase::makeList(null, $currentPage, $currentPageSize = $this->defaultPageSize, $mine);
        } else {
            $data = Kase::getCaseName($case_name, $currentPage, $currentPageSize = $this->defaultPageSize, $mine);
        }
        $package = array_merge($data, compact('currentPage', 'currentPageSize'));
        if ($request->isMethod('POST')) {
            return response()->json($package);
        } else {
            return $this->Content(function (Content $content) use ($package) {
                $package ['caseNames'] = Kase::addDiagnosesName();
                $view                  = view('winged.patients::case.index', $package);
                $content->header('病例列表');
                $content->body($view);
            });
        }
    }


    /**
     * POST 保存接口
     */
    public function store(Request $request)
    {
        $data = $request->input('data');
        $res  = Kase::addNewPatientCase($data);
        return response()->json($res);
    }

    /**
     * 编辑接口
     */
    public function edit($case)
    {
        Auth::user()->requireBasicUserInfo();
        return $this->Content(function (Content $content) use ($case) {
            // action - 1 编辑病例
            // action - 2 编辑患者
            $action = request()->input('action', 2) ;
            $res  = Kase::getCaseDetail($case);
            $view = view('winged.patients::case.edit', [
                'patient_id' => $case,
                'info' => $res['case'] ,
                'grade' => Auth::user()->getAttribute('grade'),
                'action' => $action
            ]);
            if ($action == 2) {
                $content->header('编辑病例');
            }
            else {
                $content->header('编辑患者基本信息');
            }
            $content->body($view);
        });
    }

    /**
     * 创建接口
     */
    public function create($patient_id = null)
    {
        $action = 0;    //新建
        Auth::user()->requireBasicUserInfo();
        return $this->Content(function (Content $content) use ($patient_id, $action) {
            $view = view('winged.patients::case.edit', compact('patient_id', 'action'));
            if($patient_id == null) {
                $content->header('新建病例');
            }
            else {
                $content->header('为指定患者添加病例');
            }
            $content->body($view);
        });
    }

    /**
     * @param $case 没用
     * 更新接口 put|patch
     */
    public function update($case)
    {
        return response()->json(Kase::updateCase(request()->input('data')));
    }

    /**
     * 详情接口
     */
    public function show($id)
    {
        Auth::user()->requireBasicUserInfo();
        return $this->Content(function (Content $content) use ($id) {
            $res  = Kase::getCaseDetail($id);
            $view = view('winged.patients::case.show', ['id' => $id, 'info' => $res['case'] , 'count' => $res['count']]);
            // Header信息已完全转移到前端渲染
            // $content->header($res['case']['patientName']);
            // $desc    = [];
            // $desc [] = ($res['case'] ['patientGender'] === 'MALE' ? '男' : '女');
            // $desc [] = $res['case'] ['patientAge'] . '岁';
            // $desc [] = '病案号：' . $res['case'] ['patientId'];
            // // 不再使用病例号显示
            // // $desc [] = '病例号：' . $res['case'] ['medicalRecordNo'];
            // $content->description(implode('&nbsp;&nbsp;&nbsp;&nbsp;', $desc));
            $content->body($view);
        });
    }

    /**
     * @param $case 病例的id
     * 删除接口
     */
    public function destroy($case)
    {
        return response()->json(Kase::deleteCase($case));
    }
}
